class RecipesController < ApplicationController
 before_filter :get_categories
 layout 'recipes'

  def auto_complete_for_recipe_contains
    search = params[:recipe][:contains]
    @recipe = Recipe.search(search) unless search.blank?
    render :partial => "live/search" 
  end

  def change_date
    
     @date = (params[:lastdate])
     @id = (params[:id]) 
	
     @recipe = Recipe.find(@id)
     @recipe.lastdate = @date;

     @recipe.update_attributes(params[:recipe])

  end

  def index
    @category = Category.find(:all)
     
    ##### if no categories exist create one
    if @category.size == 0
      @categoryput = Category.new
      @categoryput.category = 'Uncategorized'
      @categoryput.save
    end

    ##### sorting options 
    @list_by = (params[:by])
    if @list_by == "cat"
      @recipes = Recipe.find(:all, :order => "category_id")
    elsif @list_by == "rat"
      @recipes = Recipe.find(:all, :order => "rating")
    elsif @list_by == "dat"
      @recipes = Recipe.find(:all, :order => "lastdate")
    elsif @list_by == "tit"
      @recipes = Recipe.find(:all, :order => "title")
    else
      @recipes = Recipe.find(:all)
    end
    @categs = Category.find(:all)
    @numb = @categs.length

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @recipes }
    end
  end

############################################## show

  def show
    @recipe = Recipe.find(params[:id])
    @categs = Category.find(:all)
    @ingredients = Ingredient.find(:all, :conditions => "recipe_id = '#{@recipe.id}'")
    @size = @ingredients.length

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @recipe }
    end
  end

############################################### new

  def new
    @recipe = Recipe.new
    @ingredient = Ingredient.new
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @recipe }
    end
  end

############################################### edit

  def edit
    @recipe = Recipe.find(params[:id])
    @categs = Category.find(:all)

    @ingredients = Ingredient.find(:all, :conditions => "recipe_id = '#{@recipe.id}'")
    @size = @ingredients.length
    @sizelessone = @size - 1
    @mynumber = 1;

  end

############################################### create

  def create
    @recipe = Recipe.new(params[:recipe])
    @ingreds = (params[:ingredients])
    @amts = (params[:amounts])
    @size = @amts.length

    respond_to do |format|
      if @recipe.save

        i = 0
        @size.times do

        if @ingreds[i] != "" && @amts[i] != ""

          @ingred = Ingredient.new
          @ingred.ingredient = @ingreds[i]
          @ingred.amount = @amts[i]
          @ingred.recipe_id = @recipe.id
          @ingred.save
        end

        i += 1

        end

        @recipe.id
        flash[:notice] = 'Recipe was successfully created.'
        format.html { redirect_to(@recipe) }
        format.xml  { render :xml => @recipe, :status => :created, :location => @recipe }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
      end
    end
  end


################################################ update

  def update
    @recipe = Recipe.find(params[:id])
    Ingredient.delete_all "recipe_id = '#{@recipe.id}'"
    @ingreds = (params[:ingredients])
    @amts = (params[:amounts])
    if @amts != nil
    @size = @amts.length
    else
    @size = 0
    end
    respond_to do |format|
      if @recipe.update_attributes(params[:recipe])

        i = 0
        @size.times do

        if @ingreds[i] != "" && @amts[i] != ""

	  @ingred = Ingredient.new
	  @ingred.ingredient = @ingreds[i]
	  @ingred.amount = @amts[i]
	  @ingred.recipe_id = @recipe.id
	  @ingred.save

	end

	i += 1

        end

        flash[:notice] = 'Recipe was successfully updated.'
        format.html { redirect_to(@recipe) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @recipe.errors, :status => :unprocessable_entity }
      end
    end
  end

################################################ destroy

  def destroy

    @recipe = Recipe.find(params[:id])
    Ingredient.delete_all "recipe_id = '#{@recipe.id}'"
    @recipe.destroy

    respond_to do |format|
      format.html { redirect_to(recipes_url) }
      format.xml  { head :ok }
    end
  end

################################################ get categories

  def get_categories
    @categories = Category.find(:all)
  end

end
